﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Forms.Core;
using Umbraco.Forms.Data.Storage;
using umbraco.cms.businesslogic.member;

namespace ContourStrikesAgain.WorkflowTypes
{
    public class AddMemberToMemberGroup : WorkflowType
    {
        [Umbraco.Forms.Core.Attributes.Setting("Membergroup",
           description = "Membergroup to add",
           control = "Umbraco.Forms.Core.FieldSetting.Pickers.MemberGroup")]
        public string Group { get; set; }

        public AddMemberToMemberGroup()
        {
            this.Id = new Guid("7EA37B45-6ABF-40E6-BFA2-66817399C5E5");
            this.Name = "Add member to membergroup";
            this.Description = "Adds member to membergroup";

        }

        public override List<Exception> ValidateSettings()
        {
            List<Exception> exceptions = new List<Exception>();
          
            return exceptions;
        }

        public override Umbraco.Forms.Core.Enums.WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
        {
            SettingsStorage ss = new SettingsStorage();
            string mId = ss.GetSetting(record.Id, "SaveAsUmbracoMemberCreatedMemberID");

            int id = 0;
            int gid = 0;
            if (int.TryParse(mId, out id) && int.TryParse(Group, out gid))
            {
                Member m = new Member(id);

                m.AddGroup(gid);
            }

            return Umbraco.Forms.Core.Enums.WorkflowExecutionStatus.Completed;
        }
    }
}